package com.example.rxjavacoredemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.example.rxjavacoredemo.rxjava.Disposable;
import com.example.rxjavacoredemo.rxjava.Observable;
import com.example.rxjavacoredemo.rxjava.ObservableEmitter;
import com.example.rxjavacoredemo.rxjava.ObservableMap;
import com.example.rxjavacoredemo.rxjava.ObservableOnSubscribe;
import com.example.rxjavacoredemo.rxjava.Observer;
import com.example.rxjavacoredemo.rxjava.function.Function;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // RxJava 基础实现
      /*  Observable observable = Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("RenHui");
                emitter.onComplete();
            }
        });

        Observer observer = new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                d.dispose(false);
            }

            @Override
            public void onNext(Object o) {
                Log.e(TAG, "Next = " + o.toString());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {

            }
        };

        observable.subscribe(observer);*/

        // RxJava 操作符实现
        /*ObservableMap.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("MaoMao");
                emitter.onComplete();
            }
        }).map(new Function<String, String>() {
            @Override
            public String apply(String o) {
                return o + "，Hello";
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String o) {
                Log.e(TAG, "收到" + o);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {

            }
        });*/

        // RxJava 线程切换实现
        ObservableMap.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("MaoMao");
                emitter.onComplete();
            }
        }).map(new Function<String, String>() {
            @Override
            public String apply(String o) {
                Log.e(TAG, "apply Thread = " + Thread.currentThread().getName());
                return o + "，Hello";
            }
        }).subscribeOn().observeOn()
          .subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String o) {
                Log.e(TAG, " Next Thread = " + Thread.currentThread().getName());
                Log.e(TAG, "收到" + o);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
